<?php

namespace Home\Controller;

use Home\Biz\FieldBiz;
use Think\Controller;
use Home\Biz\AdminBiz;
use Home\Biz\OrderBiz;
use Home\Biz\ProductBiz;
use Home\Biz\SkuBiz;
use Home\Biz\StyleBiz;
use Home\Biz\CommonBiz;
use Home\Biz\PromotionBiz;

/**
 * 页面ajax请求服务处理Controller
 *
 * @author Simon
 *
 */
class AjaxServiceController extends Controller
{

    /**
     * Ajax服务中转类
     */
    public function center($moudle, $util)
    {

        CommonBiz::saveOperation(cookie('login_user_id'), 1, C('SystemConfig.current_domain'), $moudle, $util, CommonBiz::getParamsStr());

        switch (strtolower($moudle)) {
            case 'admin' :
                $this->adminService($util);
                return;
            case 'order' :
                $this->orderService($util);
                return;
            case 'product' :
                $this->productService($util);
                return;
            case 'sku' :
                $this->skuService($util);
                break;
            case 'style' :
                $this->styleService($util);
                break;
            case 'promotion' :
                $this->promotionService($util);
                break;
            case 'field' :
                $this->fieldService($util);
                break;
            default :
                $this->responseFail(5, '无法找到服务模块');
        }
    }

    /**
     * 商品字段模块
     * @param $util
     */
    public function fieldService($util)
    {
        switch (strtolower($util)) {
            case 'saveorupdate':
                $field_id = I('field_id');
                $field = array(
                    'field_name' => I('field_name'),
                    'field_code' => I('field_code'),
                    'unit' => I('unit')
                );

                if (empty($field_id)) {
                    $field ['id'] = FieldBiz::generateFieldId();
                    $field ['create_date'] = date("y-m-d H:i:s", time());
                    $field ['create_user'] = cookie('login_user_id');

                    $msg = FieldBiz::saveField($field, I('items'));
                    if (!empty($msg)) {
                        $this->responseFail(500, $msg);
                    } else {
                        $this->responseSuccess();
                    }
                    return;
                } else {
                    $field ['id'] = $field_id;
                    $field ['update_date'] = date("y-m-d H:i:s", time());
                    $field ['update_user'] = cookie('login_user_id');
                    $msg = FieldBiz::updateField($field, I('items'));
                    if (!empty($msg)) {
                        $this->responseFail(500, $msg);
                    } else {
                        $this->responseSuccess();
                    }
                    return;
                }

                break;
            case 'search':
                $result = FieldBiz::searchField(I('field_code'), I('field_name'));
                $this->responseSuccess($result);
                return;
            case 'getdetail':
                $result = FieldBiz::getDetail(I('id'));
                $this->responseSuccess($result);
                return;

            case 'deleteoption':
                $result = FieldBiz::deleteOption(I('option_id'));
                if ($result) {
                    $this->responseSuccess();
                } else {
                    $this->responseFail();
                }
                return;
            case 'loadoptions':
                $result = FieldBiz::loadOption(I('field_name'));
                if ($result) {
                    $this->responseSuccess($result);
                } else {
                    $this->responseFail();
                }
                return;
            case 'savedetail':
                $result = FieldBiz::saveDetail(I('field_line'));
                if (!$result) {
                    $this->responseSuccess();
                } else {
                    $this->responseFail($result);
                }
                return;
            case 'loaddetail':
                $result = FieldBiz::loadDetail(I('line_id'));
                if ($result) {
                    $this->responseSuccess($result);
                } else {
                    $this->responseFail();
                }
                return;
            case 'queryproductline':
                $result = FieldBiz::queryProductLine(I('product_type') , I('index') , I('pageSize'));
                if ($result) {
                    $this->responseSuccess($result);
                } else {
                    $this->responseFail();
                }
                return;
            case 'apply':
                $result = FieldBiz::apply(I('line_id'));
                $this->responseSuccess($result);
                return;
            case 'findoperationlog' :
                $result = FieldBiz::findOperationLog(I('line_id'));
                $this->responseSuccess($result);
                return;

        }
    }

    /**
     * product 模块
     *
     * @param string $util
     *            方法名
     */
    public function productService($util)
    {
        switch (strtolower($util)) {
            case 'saveorupdate' :
                $product_id = I('product_id');
                $product = array(
                    'product_name' => I('product_name'),
                    'cost_price' => I('cost_price'),
                    'material_no' => I('material_no'),
                    'image' => I('image'),
                    'image_list' => I('image_list'),
                    'material_desc' => I('material_desc'),
                    '69_code' => I('69_code'),
                    'haier_material_no' => I('haier_material_no'),
                    'remark' => I('remark'),
                    'material_length' => I('material_length'),
                    'material_width' => I('material_width'),
                    'material_height' => I('material_height'),
                    'material_volume' => I('material_volume'),
                    'material_weight' => I('material_weight'),
                    'part_shop' => I('part_shop'),
                    'status' => I('status'),
                    'product_size' => I('product_size'),
                    'product_type' => I('product_type'),
                    'product_group' => I('product_group'),
                    'product_resolving' => I('product_resolving'),
                    'product_part_group' => I('product_part_group'),
                    'part_work_shop' => I('part_work_shop'),
                    'product_part_model' => I('product_part_model')
                );
                if (empty ($product_id)) {
                    $product ['product_id'] = ProductBiz::generateProductId();
                    $product ['create_date'] = date("y-m-d H:i:s", time());
                    $product ['create_user'] = "whaleyinfo.product productBiz.addProduct";

                    $msg = ProductBiz::addProduct($product);

                    if (!empty($msg)) {
                        $this->responseFail(500, $msg);
                    } else {
                        $this->responseSuccess();
                    }
                    return;
                } else {
                    $product ['product_id'] = $product_id;
                    $product ['update_date'] = date("y-m-d H:i:s", time());
                    $product ['update_user'] = "whaleyinfo.product productBiz.updateProduct";

                    $msg = ProductBiz::updateProduct($product);
                    if (!empty($msg)) {
                        $this->responseFail(500, $msg);
                    } else {
                        $this->responseSuccess();
                    }
                    return;
                }
            case 'queryproduct' :
                $result = ProductBiz::queryProduct(I('product_name'), I('material_no'), I('69_code'), I('haier_material_no'), I('pageSize'), I('index'));
                $this->responseSuccess($result);
                return;

        }
    }


    /**
     * promotion 模块
     *
     * @param string $util
     *            方法名
     */
    public function promotionService($util)
    {
        switch (strtolower($util)) {
            case 'saveorupdate' :
                $promotion_id = I('promotion_id');
                $promotion = array(
                    'promotion_name' => I('promotion_name'),
                    'promotion_desc' => I('promotion_desc'),
                    'promotion_type' => I('promotion_type'),
                    'promotion_channel' => I('promotion_channel'),
                    'condition_count' => I('condition_count'),
                    'condition_amount' => I('condition_amount'),
                    'discount_amount' => I('discount_amount'),
                    'sku_id_list' => I('sku_id_list'),
                    'status' => I('status')
                );
                if (empty($promotion_id)) {
                    $promotion ['product_id'] = PromotionBiz::generatepromotionId();
                    $promotion ['create_date'] = date("y-m-d H:i:s", time());
                    $promotion ['create_user'] = "whaleyinfo.product PromotionBiz.addpromotion";
                    $msg = PromotionBiz::addpromotion($promotion);
                    if (!empty($msg)) {
                        $this->responseFail(500, $msg);
                    } else {
                        $this->responseSuccess();
                    }
                    return;
                } else {
                    $promotion ['promotion_id'] = $promotion_id;
                    $promotion ['update_date'] = date("y-m-d H:i:s", time());
                    $promotion ['update_user'] = "whaleyinfo.product PromotionBiz.updatepromotion";
                    $msg = PromotionBiz::updatepromotion($promotion);
                    if (!empty($msg)) {
                        $this->responseFail(500, $msg);
                    } else {
                        $this->responseSuccess();
                    }
                    return;
                }
            case 'querypromotion' :
                $result = PromotionBiz::querypromotion(I('promotion_name'), I('pageSize'), I('index'));
                $this->responseSuccess($result);
                return;
        }
    }

    /**
     * admin 模块
     *
     * @param string $util
     *            方法名
     */
    public function adminService($util)
    {
        switch (strtolower($util)) {
            case 'login' :
                // 用户登陆
                $user_id = I('param.user_id');
                $password = I('param.password');

                $error_msg = AdminBiz::login($user_id, $password);

                if (empty ($error_msg)) {
                    $this->responseSuccess();
                } else {
                    $this->responseFail(5, $error_msg);
                }
                return;
            case 'logout' :
                // 用户登陆
                $user_id = I('param.user_id');
                $error_msg = AdminBiz::logout($user_id);
                if (empty ($error_msg)) {
                    $this->responseSuccess();
                } else {
                    $this->responseFail(5, $error_msg);
                }
                return;
            default :
                $this->responseFail(5, '无法找到服务方法');
        }
    }

    /**
     * admin 模块
     *
     * @param string $util
     *            方法名
     */
    public function orderService($util)
    {
        switch (strtolower($util)) {
            case 'orderquery' :
                $thirdOrderId = I('third_order_id');
                $receiverMobile = I('receiver_mobile');
                $orderId = I('order_id');
                $reveiverAdd = I('consignee_address');
                $orderStartDate = I('order_date');
                $orderEndDate = I('order_end_time');
                $index = I('index');
                $pageSize = I('pageSize');
                $orderStatus = I('taobao_order_status');
                $cashBackStatus = I('cash_back_status');
                $invoiceKind = I('invoice_kind');
                $invoiceStatus = I('invoice_status');
                $message = I('message');
                $orderType = I('orderType');

                $consigneeName = I('consignee_name');

                $reultList = OrderBiz::queryOrderByPara($thirdOrderId, $receiverMobile, $orderId, $reveiverAdd, $orderStartDate, $orderEndDate, $orderStatus, $cashBackStatus, $invoiceKind, $invoiceStatus, $consigneeName, $message, $orderType, $index);

                $this->responseSuccess($reultList);
                return;
            case 'invoicequery' :
                $thirdOrderIds = I('third_order_ids');
                $orderStartDate = I('order_date');
                $orderEndDate = I('order_end_time');

                $cashBackStatus = I('cash_back_status');
                $message = I('message');
                $orderType = I('order_type');
                $index = I('index');
                $pageSize = I('pageSize');
                $reultList = OrderBiz::queryInvoiceByPara($thirdOrderIds, $orderStartDate, $orderEndDate, $cashBackStatus, $message, $orderType, $index);

                $this->responseSuccess($reultList);
                return;
            case 'updateinvoicename' :
                $thirdOrderIds = I('third_order_ids');
                $reultList = OrderBiz::queryUpdateInvoiceNameByPara($thirdOrderIds);
                $this->responseSuccess($reultList);
                return;
            case 'openinvoice' :
                $orderIds = I('order_ids');
                $invoiceType = I('invoice_type');
                OrderBiz::UpdateOrderInfoInvoiceStatus($orderIds, $invoiceType);

                OrderBiz::AddOrderInvoice($orderIds, $invoiceType, 'whaleyinfo.order.AjaxService.order openinvoice');

                $this->responseSuccess(null);
                return;
            case 'redinvoice' :
                $orderId = I('order_id');
                $redReason = I('red_reason');
                OrderBiz::redInvoiceBiz($orderId, $redReason);
                return;
            case 'updateinvoicetitle' :
                $thirdOrderId = I('third_order_id');
                $invoice_title = I('invoice_title');
                OrderBiz::UpdateOrderInfoInvoiceTitle($thirdOrderId, $invoice_title);

                $this->responseSuccess(null);
                return;
            case 'updateinvoicekind' :
                $thirdOrderId = I('third_order_id');
                $invoiceKind = I('invoice_Kind');
                OrderBiz::changeInvoiceKind($thirdOrderId, $invoiceKind);
                $this->responseSuccess(null);
                return;
            default :
                $this->responseFail(5, '无法找到服务方法');
        }
    }

    public function responseSuccess($result)
    {
        // 0，正确；1，参数错误；2,安全码校验失败;3，无法反序列化；4,服务器错误;5，业务逻辑错误
        $return_json = array(
            'IsSuccess' => 1,
            'ErrorMsg' => '',
            'ErrorCode' => 0,
            'Result' => $result
        );
        $this->ajaxReturn($return_json);
    }

    public function responseFail($error_code, $error_msg)
    {
        // 0，正确；1，参数错误；2,安全码校验失败;3，无法反序列化；4,服务器错误;5，业务逻辑错误
        $return_json = array(
            'IsSuccess' => 0,
            'ErrorMsg' => $error_msg,
            'ErrorCode' => $error_code,
            'Result' => null
        );
        $this->ajaxReturn($return_json);
    }

    public function skuService($util)
    {
        switch ($util) {
            case 'searchSkuList' :
                $sku_en = array(
                    'style_sn' => I('style_sn'),
                    'style_name' => I('style_name'),
                    'is_buy_fz' => I('is_buy_fz'),
                    'page_index' => I('page_index'),
                    'page_size' => I('page_size')
                );
                $return_en = SkuBiz::searchSkuList($sku_en);
                $this->responseSuccess($return_en);
                break;

            case 'updateSku' :
                $sku_id = I('sku_id');
                $sku_sn = I('sku_sn');
                $sku_name = I('sku_name');
                $style_id = I('style_id');
                $style_name = I('style_name');
                $style_sn = I('style_sn');
                $product_id = I('product_id');
                $product_name = I('product_name');
                $material_no = I('material_no');
                $sku_channel = I('sku_channel');

                $sku_channel_name = I('sku_channel_name');
                $desc_url = I('desc_url');
                /* $sku_dimension_id1 = I('sku_dimension_id1');
                $sku_dimension_name1 = I('sku_dimension_name1');
                $sku_dimension_value1 = I('sku_dimension_value1');
                $sku_dimension_id2 = I('sku_dimension_id2');
                $sku_dimension_name2 = I('sku_dimension_name2');
                $sku_dimension_value2 = I('sku_dimension_value2');
                $is_city_price = I('is_city_price'); */

                $return_price = I('return_price');
                $market_price = I('market_price');
                $dealer_min_limit_price = I('dealer_min_limit_price');
                $sale_price = I('sale_price');
                $person_commission_amount = I('person_commission_amount');
                $dealer_commission_amount = I('dealer_commission_amount');


                $image = I('image');
                $image_list = I('image_list');
                $status = I('status');
                $promotion_text = I('promotion_text');
                $priority = I('priority');
                $is_gift = I('is_gift');
                $is_rebate = I('is_rebate');

                $sku_en = array(
                    'sku_id' => $sku_id,
                    'sku_sn' => $sku_sn,
                    'sku_name' => $sku_name,
                    'style_id' => $style_id,
                    'style_name' => $style_name,
                    'product_id' => $product_id,
                    'product_name' => $product_name,
                    'style_sn' => $style_sn,
                    'priority' => $priority,
                    'material_no' => $material_no,
                    'sku_channel' => $sku_channel,
                    'promotion_text' => $promotion_text,
                    'sku_channel_name' => $sku_channel_name,
                    'desc_url' => $desc_url,
                    'is_gift' => $is_gift,
                    'is_rebate' => $is_rebate,
                    'return_price' => $return_price,
                    /* 'sku_dimension_id1' => $sku_dimension_id1,
                    'sku_dimension_name1' => $sku_dimension_name1,
                    'sku_dimension_value1' => $sku_dimension_value1,
                    'sku_dimension_id2' => $sku_dimension_id2,
                    'sku_dimension_name2' => $sku_dimension_name2,
                    'sku_dimension_value2' => $sku_dimension_value2,
                    'is_city_price' => $is_city_price, */
                    'market_price' => $market_price,
                    'dealer_min_limit_price' => $dealer_min_limit_price,
                    'sale_price' => $sale_price,
                    'person_commission_amount' => $person_commission_amount,
                    'dealer_commission_amount' => $dealer_commission_amount,
                    'image' => $image,
                    'image_list' => $image_list,
                    'status' => $status,
                    'update_user' => "whaleyFz.admin skuBiz.updateSku",
                    'update_date' => date("y-m-d H:i:s", time())
                );
                $error_msg = SkuBiz::updateSku($sku_en);

                if (empty ($error_msg)) {

                    $this->responseSuccess();
                } else {
                    $this->responseFail(5, $error_msg);
                }
                break;
            case 'addSku' :
                $sku_id = I('sku_id');
                $sku_sn = I('sku_sn');
                $sku_name = I('sku_name');
                $style_id = I('style_id');
                $style_name = I('style_name');
                $product_id = I('product_id');
                $product_name = I('product_name');
                $material_no = I('material_no');
                $sku_channel = I('sku_channel');
                $priority = I('priority');
                $promotion_text = I('promotion_text');
                $sku_channel_name = I('sku_channel_name');
                $desc_url = I('desc_url');
                $style_sn = I('style_sn');
                /* $sku_dimension_id1 = I('sku_dimension_id1');
                $sku_dimension_name1 = I('sku_dimension_name1');
                $sku_dimension_value1 = I('sku_dimension_value1');
                $sku_dimension_id2 = I('sku_dimension_id2');
                $sku_dimension_name2 = I('sku_dimension_name2');
                $sku_dimension_value2 = I('sku_dimension_value2');
                $is_city_price = I('is_city_price'); */
                $market_price = I('market_price');
                $dealer_min_limit_price = I('dealer_min_limit_price');
                $return_price = I('return_price');
                $sale_price = I('sale_price');
                $person_commission_amount = I('person_commission_amount');
                $dealer_commission_amount = I('dealer_commission_amount');
                $image = I('image');
                $image_list = I('image_list');
                $status = I('status');
                $is_gift = I('is_gift');
                $is_rebate = I('is_rebate');

                $add_sku_en = array(
                    'sku_id' => $sku_id,
                    'sku_sn' => $sku_sn,
                    'sku_name' => $sku_name,
                    'style_id' => $style_id,
                    'style_name' => $style_name,
                    'style_sn' => $style_sn,
                    'product_id' => $product_id,
                    'product_name' => $product_name,
                    'priority' => $priority,
                    'material_no' => $material_no,
                    'sku_channel' => $sku_channel,
                    'promotion_text' => $promotion_text,
                    'sku_channel_name' => $sku_channel_name,
                    'desc_url' => $desc_url,
                    /* 'sku_dimension_id1' => $sku_dimension_id1,
                    'sku_dimension_name1' => $sku_dimension_name1,
                    'sku_dimension_value1' => $sku_dimension_value1,
                    'sku_dimension_id2' => $sku_dimension_id2,
                    'sku_dimension_name2' => $sku_dimension_name2,
                    'sku_dimension_value2' => $sku_dimension_value2,
                    'is_city_price' => $is_city_price, */
                    'market_price' => $market_price,
                    'is_gift' => $is_gift,
                    'is_rebate' => $is_rebate,
                    'sale_price' => $sale_price,
                    'return_price' => $return_price,
                    'dealer_min_limit_price' => $dealer_min_limit_price,
                    'person_commission_amount' => $person_commission_amount,
                    'dealer_commission_amount' => $dealer_commission_amount,
                    'image' => $image,
                    'image_list' => $image_list,
                    'status' => $status,
                    'create_user' => "whaleyFz.admin skuBiz.addSku",
                    'create_date' => date("y-m-d H:i:s", time())
                );


                $error_msg = SkuBiz::addSku($add_sku_en);

                if (empty ($error_msg)) {

                    $this->responseSuccess();
                } else {
                    $this->responseFail(5, $error_msg);
                }
                break;
            case 'checkForm':
                $result = SkuBiz::checkProduct(I('product_id'), I('sku_channel'), I('sku_id'));
                if (empty($result)) {
                    $this->responseSuccess();
                } else {
                    $this->responseFail(5, $result);
                }
                break;
            case 'getProductId' :
                $return_en = SkuBiz::generateProductId();
                $this->responseSuccess($return_en);
                break;
            case 'getSkuId' :
                $return_en = SkuBiz::generateSkuId();
                $this->responseSuccess($return_en);
                break;
            case 'getStyleId' :
                $return_en = StyleBiz::generateStyleId();
                $this->responseSuccess($return_en);
                break;
            case 'querySkuByPara' :
                $sku_sn = I('sku_sn');
                $sku_name = I('sku_name');
                $style_name = I('style_name');
                $product_name = I('product_name');
                $sku_channel = I('sku_channel');
                $status = I('status');
                $pageSize = I('pageSize');
                $index = I('index');
                $lst_sku = SkuBiz::querySkuByPara($sku_sn, $sku_name, $style_name, $product_name, $sku_channel, $status, $pageSize, $index);

                $this->responseSuccess($lst_sku);

                break;
            case 'queryProductById' :
                $product_id = I('product_id');

                $lst_sku = SkuBiz::queryProductById($product_id);

                $this->responseSuccess($lst_sku);

                break;
            case 'queryProductInfo' :
                $product_type = I('product_type');
                $product_name = I('product_name');
                $material_no = I('material_no');
                $material_desc = I('material_desc');
                $lst_sku = SkuBiz::queryProductInfo($product_type, $product_name, $material_no, $material_desc);
                $this->responseSuccess($lst_sku);

                break;
            default :
                $this->responseFail(5, '无法找到服务方法');
                break;
        }
    }

    public function styleService($util)
    {
        switch ($util) {
            case 'addStyle' :
                $style_sn = I('style_sn');
                $style_name = I('style_name');
                $style_brief = I('style_brief');
                $sku_channel = I('sku_channel');
                $sku_channel_name = I('sku_channel_name');
                $sale_date = I('sale_date');
                $market_price = I('market_price');
                $shop_price = I('shop_price');
                $sale_min_price = I('sale_min_price');
                $sale_max_price = I('sale_max_price');
                $sku_stock = I('sku_stock');
                $is_real = I('is_real');
                $status = I('status');
                $error_msg = StyleBiz::addStyle($style_sn, $style_name, $style_brief, $sku_channel, $sku_channel_name, $sale_date, $market_price, $shop_price, $sale_min_price, $sale_max_price, $sku_stock, $is_real, $status);
                if (empty ($error_msg)) {
                    $this->responseSuccess();
                } else {
                    $this->responseFail(5, $error_msg);
                }
                break;

            case 'updateStyle' :
                $style_id = I('style_id');
                $style_sn = I('style_sn');
                $style_name = I('style_name');
                $style_brief = I('style_brief');
                $sku_channel = I('sku_channel');
                $sku_channel_name = I('sku_channel_name');
                $sale_date = I('sale_date');
                $market_price = I('market_price');
                $shop_price = I('shop_price');
                $sale_min_price = I('sale_min_price');
                $sale_max_price = I('sale_max_price');
                $sku_stock = I('sku_stock');
                $is_real = I('is_real');
                $status = I('status');
                $error_msg = StyleBiz::updateStyle($style_id, $style_sn, $style_name, $style_brief, $sku_channel, $sku_channel_name, $sale_date, $market_price, $shop_price, $sale_min_price, $sale_max_price, $sku_stock, $is_real, $status);
                if (empty ($error_msg)) {
                    $this->responseSuccess();
                } else {
                    $this->responseFail(5, $error_msg);
                }
                break;
            case 'queryStyleByPara' :
                $style_sn = I('style_sn');
                $style_name = I('style_name');
                $sku_channel = I('sku_channel');
                $pageSize = I('pageSize');
                $index = I('index');
                $lst_styles = StyleBiz::queryStyleByPara($style_sn, $style_name, $sku_channel, $pageSize, $index);

                $this->responseSuccess($lst_styles);

                break;

            default :
                $this->responseFail(5, '无法找到服务方法');
                break;
        }
    }
}